	<!doctype html>
<html>
<head>
<meta charset="utf-8">

<link href="../css/Candidates.css" rel='stylesheet' type='text/css'>
<link href="../jQueryAssets/jquery.ui.core.min.css" rel="stylesheet" type="text/css">
<link href="../jQueryAssets/jquery.ui.theme.min.css" rel="stylesheet" type="text/css">
<link href="../jQueryAssets/jquery.ui.datepicker.min.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../tinymce/js/tinymce/tinymce.min.js"></script>
<script src="../jQueryAssets/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="../jQueryAssets/jquery-ui-1.9.2.datepicker.custom.min.js" type="text/javascript"></script>
<link type='text/css' href='css/demo.css' rel='stylesheet' media='screen' />
<link type='text/css' href='css/osx.css' rel='stylesheet' media='screen' />
<script type="text/javascript" src="TimePicker/jquery.timepicker.js"></script>
<link rel="stylesheet" type="text/css" href="TimePicker/jquery.timepicker.css" />
<script type="text/javascript" src="TimePicker/lib/site.js"></script>
<script type="text/javascript">
tinymce.init({
    selector: "textarea"
 });
</script>
<?php

include('../php/db.php');

$db = new mysqli('localhost',$usercon,$passcon,'flss');

if ($_SESSION['login'] == NULL)
{
 header("Location: ../index.php");	
} 
else
{
$login = $_SESSION['login'];

$userid = mysql_query("SELECT * FROM `flss`.`accounts` WHERE `user` = '$login'");
while($rowshow = mysql_fetch_array($userid))
{
	$getid = $rowshow['account_id'];
}
$getcname = mysql_query("SELECT * FROM `flss`.`employer` WHERE `employer_id` = '$getid' ") or die(mysql_error);
$getcname2 = mysql_query("SELECT * FROM `flss`.`employer` WHERE `employer_id` = '$getid' ") or die(mysql_error);
$getrow2 = mysql_query("SELECT * FROM `flss`.`employer` WHERE `employer_id` = '$getid' ") or die(mysql_error);
$getrow3 = mysql_query("SELECT * FROM `flss`.`employer` WHERE `employer_id` = '$getid' ") or die(mysql_error);
$getrow4 = mysql_query("SELECT * FROM `flss`.`e_snap` WHERE `e_id` = '$getid' ") or die(mysql_error);
$getrow5 = mysql_query("SELECT * FROM `flss`.`e_snap` WHERE `e_id` = '$getid' ") or die(mysql_error);
$getrow6 = mysql_query("SELECT * FROM `flss`.`e_snap` WHERE `e_id` = '$getid' ") or die(mysql_error);
$getrow7 = mysql_query("SELECT * FROM `flss`.`e_snap` WHERE `e_id` = '$getid' ") or die(mysql_error);
$getrow8 = mysql_query("SELECT * FROM `flss`.`e_snap` WHERE `e_id` = '$getid' ") or die(mysql_error);
$getrow9 = mysql_query("SELECT * FROM `flss`.`e_snap` WHERE `e_id` = '$getid' ") or die(mysql_error);
$getrow10 = mysql_query("SELECT * FROM `flss`.`e_snap` WHERE `e_id` = '$getid' ") or die(mysql_error);

$countjob = mysql_query("SELECT Count(*) as Count FROM `flss`.`job_order` where `employer_ID` = '$getid' ") or die(mysql_error());
$count = mysql_fetch_assoc($countjob);
$cjob = $count['Count'];

$getalljob = mysql_query("SELECT * FROM `flss`.`job_order` where `employer_ID` = '$getid' ") or die(mysql_error());

$query = "SELECT * FROM `flss`.`job_order` WHERE `employer_id` = '$getid'";
$result = $db->query($query);

$categories[] = array("id" => 0, "val" => "Select jobs","refid"=>0);
while($row = $result->fetch_assoc()){
    $categories[] = array("id" => $row['job_id'], "val" => $row['Title'],"refid"=>$row['refid']);
}
 $jsonCats = json_encode($categories);

}

$getjobid = $_GET['job_id'];
$getdate = mysql_query("SELECT * FROM `flss`.`job_order` WHERE `job_id`='$getjobid'") or die(mysql_error()); 
$getnumapps = mysql_query("SELECT * FROM `flss`.`job_order` WHERE `job_id`='$getjobid'") or die(mysql_error()); 
					



?>

<script> 
		<?php
        echo "var categories = $jsonCats; \n";
	  	?>
		function loadCategories(){
        var select = document.getElementById("JobsName");
       	
		for(var i = 0; i < categories.length; i++){
          select.options[i] = new Option(categories[i].val,categories[i].id);    
         // select.options[i].attr('refid',categories[i].refid);
        if(select.options[i].value == "<?=$_GET['job_id']?>")
        {
            select.options[i].selected = true;
			  
        }
          

          
        }
      }
</script>

<title>
<?php  while ($cnameget = mysql_fetch_array($getcname2)) {
						 echo $cnameget['ename'];
    				}  ?> 
</title>
      
        
        
</head>
<body onLoad="loadCategories()">

<?php
 include __DIR__ . "/../php/header.php";

?>
<style>
.font{
   font-family: 'Gadugi';
}
.title{
  font-family: 'Gadugi';
  text-align:center;
  padding-top:60px;
  margin-top: 50px;
  background-color: rgb(250,250,250);
  border-bottom:1px solid #CCCCCC;
  width:100%;
  height:130px
}
</style>
<div class="title"><font size=6><b>Applicant Applied</div></font></b>
<div class="bwrapper">
	<div class="boverallcontainer">
		<div class="bheader">
        	<div class="bheadercontext">
            	<div class="bheaderleft">
                    <table>
                    <tr>
                    <td class="font">
                    <label for="Job_Order"> Job Order </label>
                    </td>
                    </tr>
                    <tr>
                    <td class="font" align="left">
                    <form method="post">
                    <Select id="JobsName" name="JobsName">
                    <option value="null" selected disabled> Select Job </option>
                    </Select>
                    <input type="hidden" id="job_id"> 
                    </form>
                    </td>
                    <td>
                    </td>
                    <td class="font">
                    <Select id="status">
                        <option value="NULL"> Select Status </option>
                        <option value="New" <?php if($_GET['status'] == 'New'){ echo "selected";} ?> >New</option>
                        <option value="For Interview" <?php if($_GET['status'] == 'For Interview'){ echo "selected";} ?> >For Interview</option>
                        <option value="For Hiring" <?php if($_GET['status'] == 'For Hiring'){ echo "selected";} ?>>For Hiring</option>
                    </select>
                    </td>
                    </tr>
					</table>
               	</div>
                <div class="bheaderright">
                	<table>
                    <tr>
                    <td class="font">
                    <label for="status_dclosing"> Closing Date:  </label>
                    </td>
					<td class="font">
                    <?php
					while($rowclosing = mysql_fetch_array($getdate))
						{
							$getclosing = $rowclosing['Job_cdate'];
							$dtime = new DateTime($getclosing);
							print $dtime->format("F d, Y");	
						} 
					?>
                    </td>
                    </tr>
                    <tr>
                    <td class="font">
	                <label for="numberofapps"> Number Of Applicants Needed: </label>
					</td>
                    <td align="center" class="font">
                    <?php
					while($rownumaps = mysql_fetch_array($getnumapps))
						{
							$getnum = $rownumaps['numapps'];
							echo $getnum;
						} 
					?>
                    
                    </td>
                    </tr>
                    </table>
                </div>
            </div>
        </div>
		<div class="btable">
        	<div class="btablecontainer">
                <div class="btablecontext">
                	<table align="center">
                    	<tr align="center">
                        <td style="width:350px;">Candidates </td>
                        <td>Status </td>
                      	<td>Date Apply </td>
                        <td>Action </td>
                      </tr>
                    	<?php
                            $browserid = $_GET['job_id'];
                            $status = $_GET['status'];
                            $where = '';
                            if(isset($status))
                            {
                                $where = " AND jaa.Status = '{$status}'";
								$phql = "SELECT a.refid,jaa.applicant_id,jaa.Job_id,jaa.Date_apply,jaa.Status,CONCAT(a.fname,' ',a.lname) as fullname FROM `flss`.`job_apply_applicant` jaa 
                                     LEFT JOIN applicant a ON a.applicant_id = jaa.Applicant_ID
                                     WHERE jaa.`Job_id` = {$browserid} ".$where;

                            $info = mysql_query($phql);
                            }
							
                            
                                   
                        ?>
                        <?php while($row = mysql_fetch_array($info)) {  ?>  
                        <tr style="background-color:#EDEDED;" align="center">
                        <td><a href="../applicant/viewapplicant.php?refid=<?=$row['refid']?>" onClick="window.open(this.href,'popupwindow','left=200,top=50,width=1000,height=600,scrollbars=yes,menu=0,status=0'); return false;"><?=$row['fullname']?></a></td>
                        <td><?=$row['Status']?></td>
                        <td><?php $app_id = $row['applicant_id']; $job_id = $row['Job_id'];  $date = new DateTime($row['Date_apply']); echo $date->format('M d, Y');?>   </td>
                        <?php 
							if($row['Status'] == "New")
							{
								    echo "<td>"."<input app_id='".$app_id."' job_id='$job_id' class='btnAcceptRegistration' type='submit' name='accept' value='Accept'><input  app_id='".$app_id."' job_id='$job_id' class='btnDeclineRegistration' type='submit' name='decline' value='Decline'>"."</td>";
                                
								}
							else if ($row['Status'] == "For Interview")
							{		$app_id = $row['applicant_id'];
									$job_id = $row['Job_id'];
									 $checkinterview = mysql_query("SELECT * FROM `flss`.`applicant_interview_date` WHERE `applicant_id` = '$app_id' AND `Job_id` = '$job_id'");
									while($ha = mysql_fetch_array($checkinterview))
									{
										$getapp = $ha['applicant_id'];
									}
										if($getapp != NULL)
										{	echo "<form method='post'>";
											echo "<td>"."<input app_id='".$app_id."' emp_id='".$emp_id."' job_id='".$job_id."' class='btnHire' type='submit' name='accept' value='HIRE'><input app_id='".$app_id."' job_id='".$job_id."' class='btnDeclineRegistration' type='submit' name='decline' value='Decline'>"."</td>";
											echo "</form>";
											}
										else
										{
											echo "<td>"."<input app_id='".$app_id."' job_id='".$job_id."' class='osx demo btnSetInterviewDate' type='submit' name='accept' value='Set interview date'>"."</td>";
											
											}
							}
							else
							{
								?>
                                <td>
                                	<form method="post">
                                    <input type="submit" class="btnDeploy" value="Deploy"  name="deploy"> 
                                    </form>
                                </td>
                                
                                <?php
								if(isset($_POST['deploy']))
								{
									$today = date('Y-m-d');
									mysql_query("INSERT INTO `flss`.`hired_applicants` (`applicant_id`, `employer_id`, `job_id`, `date_hired`) VALUES ('$app_id', '$getid', '$job_id', '$today')") or die(mysql_error());
									$numapp = mysql_fetch_array(mysql_query("SELECT * FROM job_order where job_id = '$job_id' "));
									$decrease = $numapp['numapps'];
									$decrease = $decrease - 1;
									mysql_query("UPDATE job_order SET numapps = '$decrease' WHERE job_id = '$job_id'");
									mysql_query("UPDATE job_apply_applicant SET Status = 'HIRE' WHERE applicant_id = '$app_id'");
									$checkapp = mysql_fetch_array(mysql_query("SELECT * FROM job_order where job_id = '$job_id' "));
									$check = $checkapp['numapps'];
									if($check == 0)
									{
										mysql_query("UPDATE job_order SET Status = '4' WHERE job_id = '$job_id'");
										
										}
									header("Refresh:0");
									}
								}
                            
                        ?>

                        </tr>

                        <?php } ?>
             
                    </table>
                </div>
        	</div>
        </div>
      <div id="osx-modal-content">
			<div id="osx-modal-title">Interview</div>
			<div class="close"><a href="#" class="simplemodal-close">x</a></div>
			<div id="osx-modal-data">
			  <table>
              <tr>
              <td>
              <p>Interview Date</p>
              </td>
              <td>
              <p><input type="text" id="dteInterviewDate" class="dp" placeholder="yyyy-dd-mm"> </p>
              </td>
              </tr>
              <tr>
              <td>
              <p>Time of Interview: </p>
              </td>
              <td><p>
                    Starting from
                    <input  type="text" id="timein" class="time from"/>
                    to
                    <input  type="text" id="timeout" class="time to" />
					</p> 
              </td>
              <tr>
              <td>
              <p>Type of Interview</p>
              </td>
              <td>
              <p>
              <select id="toi">
              		<option value="null"> Select Type of Interview </option>
              		<option value="skype"> Skype </option>
                    <option value="FaceToFace"> Face To Face </option>
              </select>
              </p>
              </td>
              </tr>
              <tr>
              <td>
              <p>Select FLSS Nearest Location </p>
              </td>
              <td>
              <p>
              <select id="NL">
                <option value="null"> Select Nearest Location </option>  
              	<option value="Philippines">Philippines </option>
                <option value="Dubai"> Dubai </option>
              	<option value="Bangladesh"> Bangladesh </option>
                <option value="Indonesia"> Indonesia </option>
                <option value="SRI LANKA"> SRI LANKA </option>
                <option value="Nepal"> Nepal </option>  
                <option value="Pakistan"> Pakistan </option>
                <option value="India"> India </option>  
              </select>
              </p>
              </td>
              </tr>
              <tr>
              <td>
              <p><input type="submit" value="Save" class="btnSaveInterviewDate"></p>
              </td>
              </tr>
              </table>		    
              <p><button class="simplemodal-close">Close</button> <span>(or press ESC or click the overlay)</span></p>
			</div>
		</div>
	  
  </div>
</div>
					<script type="text/javascript">
					$(function() {
                        function setForInterview($applicant_id,$job_id,$interview_date,$type,$from,$to,$nearest){
                            var params = {};
                            params.applicant_id = $applicant_id;
                            params.job_id = $job_id;
                            params.interview_date = $interview_date;
                            params.type = $type;
                            params.from = $from;
                            params.to = $to;  
                            params.nearest = $nearest;

                            return $.ajax({
                                url: '../api/set_for_interview.php',
                                data: params,
                                dataType: 'json',
                                type: 'post'
                            });
                        }

                        $(document).ready(function(){
                            $(document).on('click','.btnSaveInterviewDate',function(){
                                var applicant_id = $(this).attr('app_id');
                                var job_id = $(this).attr('job_id');
                                var from = $('.from').val();
                                var to = $('.to').val();
                                var toi = $('#toi option:selected').val();
                                var nl = $('#NL option:selected').val();
                                var interview_date = $('#dteInterviewDate').val();


                                if(toi == "null" || nl == "null" || from=="" || to == ""){
                                    alert("Please fill all fields");
                                    return;
                                }

                                setForInterview(applicant_id,job_id,interview_date,toi,from,to,nl).done(function(result){
                                    console.log(result);  
                                    if(result.status){
                                       window.location.reload();
                                    }else{
                                      alert(result.status);
                                    }
                                });
                            });
                            $(document).on('click','.btnSetInterviewDate',function(){
                                $('.btnSaveInterviewDate').attr('app_id','');
                                $('.btnSaveInterviewDate').attr('job_id','');
                                var applicant_id = $(this).attr('app_id');
                                var job_id = $(this).attr('job_id');
                                $('.btnSaveInterviewDate').attr('app_id',applicant_id);
                                $('.btnSaveInterviewDate').attr('job_id',job_id);
                            });

                            $(document).on('click','.btnDeclineRegistration',function(){
                                var job_id = $(this).attr('job_id');
                                var applicant_id = $(this).attr('app_id');
                                var params = {};
                                var cur = $(this).parent();
                                var thix=$(this).html();

                                params.job_id = job_id;
                                params.applicant_id = applicant_id;
                                var r = confirm('Are you sure you want to delete?');
                                if(r){
                                    cur.html('Processing <img src="../images/loading.gif"/>');
                                    $.ajax({
                                        url: '../api/decline_applicant_registration.php',
                                        data: params,
                                        dataType:'json',
                                        type: 'post',
                                        success:function(result){
                                            if(result.status){
                                                cur.	html('Declined');
                                                window.location.reload();
                                            }else{
                                                alert('Saving failed please try again later');
                                                cur.html(thix);
                                            }
                                        }
                                    });
                                }
                            });

                            $(document).on('click','.btnAcceptRegistration',function(){
                                var job_id = $(this).attr('job_id');
                                var applicant_id = $(this).attr('app_id');
                                var params = {};
                                var cur = $(this).parent();
                                var thix=$(this).html();

                                params.job_id = job_id;
                                params.applicant_id = applicant_id;
                                var r = confirm('Are you sure you want to accept?');
                                
                                if(r){
                                    cur.html('Processing <img src="../images/loading.gif"/>');
                                    $.ajax({
                                        url: '../api/accept_applicant_registration.php',
                                        data: params,
                                        dataType:'json',
                                        type: 'post',
                                        success:function(result){
                                            if(result.status){
                                                cur.html('Accepted');
                                                window.location.reload();
                                            }else{
                                                alert('Saving failed please try again later');
                                                cur.html(thix);
                                            }
                                        }
                                    });
                                }
                            });

							$(document).on('click','.btnHire',function(){
                                var job_id = $(this).attr('job_id');
                                var applicant_id = $(this).attr('app_id');
								var emp_id = $(this).attr('emp_id');
                                var params = {};
                                var cur = $(this).parent();
                                var thix=$(this).html();

                                params.job_id = job_id;
                                params.applicant_id = applicant_id;
                                var r = confirm('Are you sure you want to hire?');
                                
                                if(r){
                                    cur.html('Processing <img src="../images/loading.gif"/>');
                                    $.ajax({
                                        url: '../api/hire_applicant.php',
                                        data: params,
                                        dataType:'json',
                                        type: 'post',
                                        success:function(result){
                                            if(result.status){
                                                cur.html('Hired');
                                                window.location.reload();
                                            }else{
                                                alert('Saving failed please try again later');
                                                cur.html(thix);
                                            }
                                        }
                                    });
                                }
                            });


                            $(document).on('click','.setForInterview',function(){
                                var job_id = $(this).attr('job_id');
                                var applicant_id = $(this).attr('app_id');
                                
                            });
					             $(document).on('change','#JobsName',function(){
                                var job_id = $( "#JobsName option:selected").val();
                                window.location.replace('candidates.php?job_id=' + job_id);
                            });

                            $(document).on('change','#status',function(){
                                var status = $( "#status option:selected").val();    
                                if(status != "Select Status")
                                {
                                    var job_id = $( "#JobsName option:selected").val();
                                    window.location.replace('candidates.php?job_id=' + job_id + '&status=' + status);
                                }
                            });
                        });
                    });
						
                    </script>
<script type='text/javascript' src='js/jquery.simplemodal.js'></script>
<script type='text/javascript' src='js/osx.js'></script>
<script type="text/javascript">
$(function() {
						var pickerOpts = {
										minDate: new Date(),
										maxDate: "+30",
										dateFormat: "yy-mm-dd"
									};
$( "#dteInterviewDate" ).datepicker(pickerOpts);  
});
</script>
<script>
                $(function() {
                    $('#timein').timepicker();
					$('#timeout').timepicker();
                });
            </script>
</body>
</html>